# -*- coding: utf-8 -*
'''
    Created by XiaoInk at 2021/11/10 22:24:06
    Gitee: https://gitee.com/XiaoInk
'''

from email import encoders
from email.header import Header
from email.mime.text import MIMEText
from email.utils import parseaddr, formataddr

import smtplib
from typing import Any, List


class Email(object):

    def __init__(self, from_addr: str, password: str):
        self.smtp = smtplib.SMTP('smtp.163.com', 25)
        self.from_addr = from_addr
        self.password = password

    def sendmail(self, to_addrs: str or List[str], msg: Any, debug: bool = False):
        if debug:
            self.smtp.set_debuglevel(1)

        msg = MIMEText(msg, 'plain', 'utf-8')
        msg['From'] = self._format_addr('AIPhoto <%s>' % self.from_addr)
        msg['To'] = self._format_addr('管理员 <%s>' % 'wangxiaoqiangs@foxmail.com')
        msg['Subject'] = Header('来自SMTP的问候……', 'utf-8').encode()

        self.smtp.login(self.from_addr, to_addrs, msg)
        self.smtp.quit()

    def _format_addr(s):
        name, addr = parseaddr(s)
        return formataddr((Header(name, 'utf-8').encode(), addr))


if __name__ == '__main__':
    Email('wangxiaoqiang888@163.com', 'KDUTCSPTQKKNRHIV').sendmail(['wangxiaoqiangs@foxmail.com'], '你好，来自Python')
